﻿using Coldairarrow.Util;
using JNPF.Logging;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using PARK.CardAcct.Entitys;
using PARK.Common.Core.Manager;
using PARK.Common.Enum;
using PARK.Common.Extension;
using PARK.Common.Filter;
using PARK.Consumer.Entity;
using PARK.Consumer.Entitys;
using PARK.Consumer.Entitys.Dto.ConsumBill;
using PARK.Consumer.Entitys.Dto.ConsumMenus;
using PARK.Consumer.Entitys.Dto.Wechat;
using PARK.Consumer.Interfaces.ConsumBill;
using PARK.Dependency;
using PARK.DynamicApiController;
using PARK.FriendlyException;
using PARK.JsonSerialization;
using PARK.System.Interfaces.System;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace PARK.Consumer
{
    /// <summary>
    /// 就餐菜单服务
    /// </summary>
    [ApiDescriptionSettings(Tag = "Wechat", Name = "ConsMenu", Order = 200)]
    [Route("api/wechat/[controller]")]
    public class WechatConsMenuService : IWechatConsMenuService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<ConsumMenusEntity> _consumMenusRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;
        /// <summary>
        /// 初始化一个<see cref="WechatConsMenuService"/>类型的新实例
        /// </summary>
        public WechatConsMenuService(
            ISqlSugarRepository<ConsumMenusEntity> consumMenusRepository,
            IUserManager userManager,
            ISysConfigService sysConfigService)
        {
            _consumMenusRepository = consumMenusRepository;
            _db = _consumMenusRepository.Context;
            _userManager = userManager;
        }

        #region 获取菜单
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="weekId"></param>
        /// <returns></returns>
        [HttpGet("GetList")]
        public async Task<dynamic> GetList(string weekId)
        {
            var userInfo = await _userManager.GetUserInfo();
            var entityEmp = await _db.Queryable<EmployeeEntity>().FirstAsync(p => p.EmployeeId == userInfo.EmployeeId);           
            if (entityEmp!=null)
            {
                List<ConsumMenusWechatOutput> output = new List<ConsumMenusWechatOutput>();
                var data = await _db.Queryable<ConsumMenusEntity>()
                //公司权限
                .Where(p => p.CompanyId.Equals(entityEmp.CompanyId)).OrderBy(" sortcode asc").ToListAsync();
                foreach (var item in data)
                {
                    ConsumMenusWechatOutput TimeInterval = new ConsumMenusWechatOutput();
                    TimeInterval.timeIntervalId = item.TimeIntervalId;
                    TimeInterval.timeIntervalName = item.TimeIntervalName;
                    var _MenusTxt = "";
                    switch (weekId)
                    {
                        case "1":
                            _MenusTxt = item.Week1;
                            break;
                        case "2":
                            _MenusTxt = item.Week2;
                            break;
                        case "3":
                            _MenusTxt = item.Week3;
                            break;
                        case "4":
                            _MenusTxt = item.Week4;
                            break;
                        case "5":
                            _MenusTxt = item.Week5;
                            break;
                        case "6":
                            _MenusTxt = item.Week6;
                            break;
                        case "7":
                            _MenusTxt = item.Week7;
                            break;
                    }
                    if (!string.IsNullOrEmpty(_MenusTxt))
                    {
                        List<string> MenuList = new List<string>();
                        string[] _Menus = _MenusTxt.Split(" ".ToCharArray());
                        for (int i = 0; i < _Menus.Length; i++)
                        {
                            MenuList.Add(_Menus[i]);
                        }
                        TimeInterval.ConsumMenus = MenuList;
                    }
                    output.Add(TimeInterval);
                }
                return output;
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
